package com.kexin.coupon.timer;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import com.kexin.common.utils.ApplicationContextUtil;
import com.kexin.coupon.service.ICouponOrderService;


/**
 * 定时任务
 */
@Lazy
@Component
public class TimerTask implements SchedulingConfigurer {

	Logger log = LoggerFactory.getLogger(TimerTask.class);

	private final String everyday = TimerConfig.getEveryday();

	private final String everydayFront = TimerConfig.getEverydayFront();

	private final String everyHour = TimerConfig.getEveryHour();

	/**
	 * 每日0时后的定时任务
	 */
	private void everydayTask() {
		log.info("每日0时后的定时任务开始["+everyday+"]");
		ICouponOrderService couponOrderService 
				= ApplicationContextUtil.getCouponOrderService();
		
		try {
			//过期未使用的优惠券自动退款
			couponOrderService.refundOutOfDateOrders();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("定时任务：过期未使用的优惠券自动退款失败", e);
		}
		log.info("每日0时后的定时任务结束["+everyday+"]");
	}

	/**
	 * 每日24时前的定时任务
	 */
	private void everydayTaskFront() {
		log.info("每日24时前的定时任务开始["+everydayFront+"]");
		ICouponOrderService couponOrderService 
				= ApplicationContextUtil.getCouponOrderService();
		
		try {
			//自动处理之前退款失败的订单
			couponOrderService.refundFailOrders();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("定时任务：自动处理之前退款失败的订单失败", e);
		}
		log.info("每日24时前的定时任务结束["+everydayFront+"]");
	}

	/**
	 * 每小时的定时任务
	 */
	private void everyHourTask() {
		log.info("每小时的定时任务开始["+everyHour+"]");
		ICouponOrderService couponOrderService 
				= ApplicationContextUtil.getCouponOrderService();
		
		try {
			//取消过期未支付的订单
			couponOrderService.cancelNotPayOrders();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("定时任务：自动取消过期未支付的订单失败", e);
		}
		
		try {
			//自动处理正在退款的订单
			couponOrderService.updateRefundingOrders();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("定时任务：自动处理正在退款的订单失败", e);
		}
		log.info("每小时的定时任务结束["+everyHour+"]");
	}

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.addTriggerTask(new Runnable() {
			
			@Override
			public void run() {
				everydayTask();
			}
		}, new Trigger() {
			
			@Override
			public Date nextExecutionTime(TriggerContext triggerContext) {
				log.info("配置每日0时后的定时任务执行时间["+everyday+"]");
				CronTrigger trigger = new CronTrigger(everyday);
				Date nextExecDate = trigger.nextExecutionTime(triggerContext);
				return nextExecDate;
			}
		});
		
		taskRegistrar.addTriggerTask(new Runnable() {
			
			@Override
			public void run() {
				everydayTaskFront();
			}
		}, new Trigger() {
			
			@Override
			public Date nextExecutionTime(TriggerContext triggerContext) {
				log.info("配置每日24时前的定时任务执行时间["+everydayFront+"]");
				CronTrigger trigger = new CronTrigger(everydayFront);
				Date nextExecDate = trigger.nextExecutionTime(triggerContext);
				return nextExecDate;
			}
		});
		
		taskRegistrar.addTriggerTask(new Runnable() {
			
			@Override
			public void run() {
				everyHourTask();
			}
		}, new Trigger() {
			
			@Override
			public Date nextExecutionTime(TriggerContext triggerContext) {
				log.info("配置每小时的定时任务执行时间["+everyHour+"]");
				CronTrigger trigger = new CronTrigger(everyHour);
				Date nextExecDate = trigger.nextExecutionTime(triggerContext);
				return nextExecDate;
			}
		});
	}
}
